package com.lbl.third.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbl.third.common.R;
import com.lbl.third.enytity.Employee;
import com.lbl.third.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;



@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;



    /**
     * 登录功能
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //将页面提交的密码password进行m5加密处理
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页提交的用户名进行数据库查询
        LambdaQueryWrapper<Employee> qw=new LambdaQueryWrapper<Employee>();
        qw.eq(Employee::getUsername,employee.getUsername());

        //3.如果没有查询到则返回登陆失败结果
        Employee one = employeeService.getOne(qw);
        if(one==null){
            return R.error("登陆失败,用户名错误");
        }

        //4.密码对比，如果不一样则返回登陆失败结果
        if(!password.equals(one.getPassword())){
            return R.error("登陆失败,密码错误");
        }

        //5.查询员工状态，如果为禁用状态（Status=0）,则返回员工账号被冻结结果
        if(one.getStatus()!=1){
            return R.error("该账号已被冻结");
        }
        //6.登陆成功，将员工id存入Session并返回登陆成功结果
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);

    }



    /**
     * 退出系统
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public  R<String>  logout(HttpServletRequest request){

        //清空Session中保存的当前员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }



    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */

    @PostMapping
    public R<String> save(  HttpServletRequest request,@RequestBody Employee employee){
        //设置初始密码，md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //获取当前操作者id
//        Long Id =(Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(Id);
//        employee.setUpdateUser(Id);



        //保存数据
        employeeService.save(employee);
        return R.success("保存成功");
    }



    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //构造分页构造器
        Page page1=new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);


        //执行查询
        employeeService.page(page1,queryWrapper);


        return R.success(page1);
    }



    /**
     * 根据Id修改员工信息
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String>  update(@RequestBody Employee employee,HttpServletRequest request){

        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
//        Long id=Thread.currentThread().getId();
//        log.info("更新1线程id"+id);
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }


    /**
     * 根据Id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> GetById(@PathVariable Long id){

        Employee employee = employeeService.getById(id);

        return R.success(employee);

    }
}
